package com.example.juc.until;

import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//验证
public class Jmm {
    //AtomicInteger保证原子性
    private   static AtomicInteger num =new AtomicInteger();
    //private static volatile num =0;
    //void add(){num++;}
    static void add(){
        num.getAndIncrement();//+1
    }
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger num =new AtomicInteger(2020);
        //到达期望值就就更新 要不就不更新
        num.compareAndSet(2020,2021);
        for (int i = 1; i <=10 ; i++) {
            new Thread(() -> {
                for (int j = 1; j <=10 ; j++) {
                      add();
                }
            },i+"").start();
        }
        while (Thread.activeCount()>2){ //最少剩两个 main gc 所有的用户线程都走完 计算num数量
            Thread.yield();
        }
        System.out.println(num);
    }
}
